/**
  grid-mask.c - Created by Timothy Morey on 6/1/2012
*/

#include <netcdf.h>
#include <string.h>

#include "data-schema.h"
#include "grid-mask.h"
#include "logger.h"
#include "netcdf-utils.h"

#ifdef __WINDOWS__
#pragma warning(disable:4996)
#endif

int ParseGridMaskDef(xmlNodePtr root, GridMaskDef* mask)
{
  int retval = 0;
  xmlChar* value;
  xmlNodePtr child = 0;

  if(root && mask)
  {
    memset(mask, 0, sizeof(GridMaskDef));

    value = xmlGetProp(root, BAD_CAST "Negative");
    mask->Negative = atoi((char*)value);
    xmlFree(value);

    child = root->children;
    while(child)
    {
      if(0 == xmlStrcmp(BAD_CAST "MaskMin", child->name))
        ParseVariant(child, &mask->MaskMin);

      else if(0 == xmlStrcmp(BAD_CAST "MaskMax", child->name))
        ParseVariant(child, &mask->MaskMax);

      child = child->next;
    }

    retval = 1;
  }

  return retval;
}

int SaveGridMaskDef(GridMaskDef mask, xmlNodePtr* root)
{
  int retval = 0;
  int i = 0;
  char temp[32];
  xmlNodePtr child = 0;

  if(root)
  {
    if(! *root)
      *root = xmlNewNode(0, BAD_CAST "GridMaskDef");

    sprintf(temp, "%d", mask.Negative);
    xmlNewProp(*root, BAD_CAST "Negative", BAD_CAST temp);

    child = xmlNewNode(0, BAD_CAST "MaskMin");
    SaveVariant(mask.MaskMin, &child);
    xmlAddChild(*root, child);

    child = xmlNewNode(0, BAD_CAST "MaskMax");
    SaveVariant(mask.MaskMax, &child);
    xmlAddChild(*root, child);

    retval = 1;
  }

  return retval;
}